var FORMAT_12_HOUR = 0;
var FORMAT_24_HOUR = 1;

var myClock = {
	currentTime: new Date(),
	timeFormat: FORMAT_12_HOUR,
	showSeconds: false,
	dateFormatString: "longDate",
		
	updateClock: function() {
		this.currentTime = new Date();
		$("#time").html(this.formatTime());
		$("#date").html(this.formatDate());
	},	
	
	formatTime: function() {
		var hours = this.currentTime.getHours();
		var minutes = this.currentTime.getMinutes();
		var seconds = this.currentTime.getSeconds();
		var ampm = "";
		
		if (this.timeFormat == FORMAT_12_HOUR) {
			if (hours > 12) {
				hours = hours - 12;
				ampm = " PM";
			} else if (hours == 0) {
				hours = 12;
				ampm = " AM";
			} else {
				ampm = " AM";
			}
		} else {
			hours = (hours < 10) ? "0" + hours : hours;
		}
		
		var timeStr = hours + ":" + ((minutes < 10) ? "0" + minutes : minutes);
		if (this.showSeconds) {
			timeStr += ":" + ((seconds < 10) ? "0" + seconds : seconds);
		}
		timeStr += ampm;
		
		return timeStr;
	},
	
	formatDate: function() {
		return dateFormat(this.currentDate, this.dateFormatString);
	}
}

function initClock() {
	$("#clock").corner();
	
	$("#time").html(myClock.formatTime());
	$("#date").html(myClock.formatDate());

	var prefs = new gadgets.Prefs();
	var theme = prefs.getString("theme");
	$("#clock").addClass(theme);
	
	var timer = setInterval(function() { myClock.updateClock(); }, 1000);
	$("#clock").fadeIn(1000);
}

